<template>
    <view v-if="showPage">
        <web-view :src="src" :update-title="false" @message="getMessage"></web-view>
    </view>
</template>

<script>
	import { getCollectionId, getFileByCollId, fileDownload, deleteCollByFileId } from '@/api/base/file.js'
	import { notice, file } from '@/utils/common.js'
	import config from '@/utils/config/index.js'
	
    export default {
        data() {
            return {
				showPage: false,
				src: '/hybrid/html/editor/editor.html',
				content: '',
				type: '',
				fileObj: {}
            }
        },
		onLoad(option) {
			this.type = option.type
			this.fileObj = JSON.parse(decodeURIComponent(option.fileObj))
			uni.setNavigationBarTitle({
				title: this.fileObj.title
			})
			this.handleData()
		},
		onShow() {
			// #ifdef APP-PLUS
			// 切换为横屏
			plus.screen.lockOrientation('landscape-primary');
			// #endif
		},
		methods: {
			async handleData() {
				if(!this.fileObj.collectionId) {
					await getCollectionId().then(res => {
						this.fileObj.collectionId = res.data
						const eventChannel = this.getOpenerEventChannel()
						eventChannel.emit('getCollectionId', this.fileObj.collectionId);
					})
				}

				this.fileObj.baseUrl = config.baseUrl
				this.fileObj.userName = uni.getStorageSync("userName")
				this.src += '?type=' + this.type + '&fileObj=' + encodeURIComponent(JSON.stringify(this.fileObj))
				this.showPage = true
				// await getFileByCollId(this.fileObj.collectionId).then(async res => {
				// 	if (res.data && res.data.length > 0) {
				// 		this.fileObj.fileId = res.data[0].fileId
				// 		await fileDownload(res.data[0].fileId).then(ret => {
				// 			this.content = this.Utf8ArrayToStr(new Uint8Array(ret))
				// 		})
				// 	}
				// })
				// setTimeout(() => {
				// 	this.sendData(JSON.stringify(this.content))
				// }, 200)
			},
			Utf8ArrayToStr(array) {
				var out, i, len, c;
				var char2, char3;
				
				out = "";
				len = array.length;
				i = 0;
				while(i < len) {
					c = array[i++];
					switch(c >> 4) {
						case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
							// 0xxxxxxx
							out += String.fromCharCode(c);
							break;
						case 12: case 13:
							// 110x xxxx 10xx xxxx
							char2 = array[i++];
							out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
							break;
						case 14:
							// 1110 xxxx 10xx xxxx 10xx xxxx
							char2 = array[i++];
							char3 = array[i++];
							out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
							break;
					}
				}
				return out;
			},
			sendData(data) {
				// #ifdef APP-PLUS
				const vw = this.$scope.$getAppWebview().children()[0]
				vw.evalJS("receiveData('" + data + "')")
				// #endif
			},
			async getMessage(e) {
				let data = e.detail.data[0].data
				let flag = data.flag
				let action = data.action
				if(flag) {
					if(action == 'save') {
						notice.toast(this.$t('baseCommon.message.saveSuccess'))
					}
					
					await getFileByCollId(this.fileObj.collectionId).then(async res => {
						if (res.data && res.data.length > 0) {
							let fileId = res.data[0].fileId
							// 删除原文件
							deleteCollByFileId({
								fileId: fileId,
								collectionId: this.fileObj.collectionId
							})
						}
					})
				} else {
					notice.toast(data.msg)
				}
			}
		}
    }
</script>

<style>

</style>
